package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.OneClass;
import com.luo.model.Result;
import com.luo.model.TwoClass;
import com.luo.service.ClassService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/1/16 20:17
 */
@WebServlet("/addOneClass")
public class AddClassController extends HttpServlet {

    ClassService classService = new ClassService();

    @Override
    protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String picture = req.getParameter("picture");
        String oneClassId = req.getParameter("oneClassId");

        if(oneClassId == null || Objects.equals(oneClassId,"")){
            OneClass oneClass = new OneClass();
            oneClass.setName(name);
            oneClass.setPicture(picture);
            classService.AddOneClass(oneClass);
        }else{
            TwoClass twoClass = new TwoClass();
            twoClass.setName(name);
            twoClass.setPicture(picture);
            twoClass.setOneClassId(oneClassId);
            classService.AddTwoClass(twoClass);
        }
        Result result = new Result();
        result.setCode("1");
        result.setMsg("添加成功");
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
